﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using System.Data;
using Telerik.Web.UI;
using System.Configuration;

namespace Web.Loyalty
{
    public partial class EditLoyalty : System.Web.UI.Page
    {
        static string prevPage = String.Empty;
        static string requestEdit = string.Empty;
        static LOYALTY loy;
        static long? assignedToID = null;
        static USER user;
        static ROLE r;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            assignedToID = null;
            if (Request.UrlReferrer != null)
            {
                prevPage = Request.UrlReferrer.ToString();
                hdntxtPrevPage.Value = prevPage;
            }
            user = (USER)Session["User"];
            if (user != null)
            {
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if (((bool)user.IS_ADMIN) || ((bool)r.IS_MANAGER))
                {
                    r = RoleService.GetRoleByID((long)user.ROLE_ID);
                    requestEdit = Request.QueryString["id"].Trim();
                    LoadAllUser();
                    LoadLoyaltyInfo();
                }
                else
                {
                     Response.Redirect("../Home.aspx");
                }
            }
        }
        private void LoadAllUser()
        {
            ddlAssignedTo.Items.Clear();
            List<USER> users = UserService.ListAll();
            if (users != null)
            {
                foreach (USER u in users)
                {
                    ROLE r2 = RoleService.GetRoleByID((long)u.ROLE_ID);
                    if (((bool)u.IS_ADMIN) || ((bool)r2.IS_MANAGER))
                    {
                        ddlAssignedTo.Items.Add(new RadComboBoxItem(u.SUR_NAME + " " + u.NAME, u.ID.ToString()));
                    }
                }
            }
        }
        private void LoadLoyaltyInfo()
        {
            try
            {
                loy = LoyaltyService.GetLoyaltyByID(Convert.ToInt64(requestEdit));
                txtName.Text = loy.NAME;
                txtDescription.Text = loy.DESCRIPTION;
                rntxtPointToAward.Value = loy.POINTS_TO_AWARD;
                rntxtExchangeRate.Value = loy.EXCHANGE_RATE;
                RadComboBoxItem ritem = ddlAssignedTo.FindItemByValue(loy.ASSIGN_TO_ID.ToString());
                if (ritem != null)
                {
                    ddlAssignedTo.ClearSelection();
                    ritem.Selected = true;
                }
            }
            catch
            {
            }
        }
        private int updateInvoice()
        {
            LOYALTY newLoy = new LOYALTY();
            newLoy.ID = loy.ID;
            DateTime dt = DateTime.Now;
            newLoy.DATE_MODIFIED = dt;
            newLoy.DATE_CREATED = loy.DATE_CREATED;
            //Details
            newLoy.NAME = txtName.Text;
            newLoy.DESCRIPTION = txtDescription.Text;
            newLoy.POINTS_TO_AWARD = rntxtPointToAward.Value;
            newLoy.EXCHANGE_RATE = rntxtExchangeRate.Value;
            if ((bool)user.IS_ADMIN)
            {
                if (ddlAssignedTo.SelectedValue != "-1")
                {
                    newLoy.ASSIGN_TO_ID = Convert.ToInt64(ddlAssignedTo.SelectedValue);
                    newLoy.ASSIGN_TO_NAME = ddlAssignedTo.SelectedItem.Text;
                }
            }
            //else
            //{
            //    newLoy.ASSIGN_TO_ID = user.ID;
            //    newLoy.ASSIGN_TO_NAME = user.SUR_NAME + " " + user.NAME;
            //}
            return LoyaltyService.UpdateLoyalty(newLoy);
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int updateInv = updateInvoice();
            if (updateInv == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Update loyalty successful !" + "','" + "Loyalty.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Update loyalty successful !", "Loyalty.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not update loyalty !" + "');");
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not update loyalty !');", true);
        }
    }
}